
package com.yimixian.app.common;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.yimixian.app.R;
import com.yimixian.app.model.Coordinate;

import android.os.Bundle;
import android.text.TextUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MarkLocationMapActivity extends YmxTitleBarActivity {

    public static final String EXTRA_COORDINATE = "extra_coordinate";
    public static final String EXTRA_TITLE = "extra_title";
    public static final String EXTRA_LEVEL = "extra_level";
    @InjectView(R.id.map_view)
    MapView mMapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        Coordinate coordinate = getIntent().getParcelableExtra(EXTRA_COORDINATE);
        Bundle bundle = getIntent().getExtras();
        if (bundle == null) {
            finish();
            return;
        }
        Coordinate coordinate = (Coordinate) bundle.get(EXTRA_COORDINATE);
        if(coordinate == null) {
            finish();
            return;
        }
        if (TextUtils.isEmpty(coordinate.latitude) || TextUtils.isEmpty(coordinate.longitude)) {
            finish();
        }
        setContentView(R.layout.mark_location_map_activity);
        ButterKnife.inject(this);

        String title = getIntent().getStringExtra(EXTRA_TITLE);
        if (!TextUtils.isEmpty(title)) {
            setTitle(title);
        }

        mMapView.showZoomControls(false); // 隐藏比例尺控件
        mMapView.showScaleControl(false); // 隐藏缩放按钮

        BaiduMap mBaiduMap = mMapView.getMap();

        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

        float zoomLevel = 16.0f;
        String level = getIntent().getStringExtra(EXTRA_LEVEL);
        if (!TextUtils.isEmpty(level)) {
            try {
                zoomLevel = Float.parseFloat(level);
            } catch (Exception e) {
            }
        }
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.zoomTo(19.0f);// 缩放级别
                                                                               // [3,
                                                                               // 20]
        mBaiduMap.setMapStatus(mapStatusUpdate);

        mBaiduMap.getUiSettings().setOverlookingGesturesEnabled(false);
        mBaiduMap.getUiSettings().setRotateGesturesEnabled(false);
        mBaiduMap.getUiSettings().setCompassEnabled(false);

        LatLng latLng = new LatLng(Double.valueOf(coordinate.latitude),
                Double.valueOf(coordinate.longitude));
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(latLng));

        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory
                .fromResource(R.drawable.map_location_icon);
        OverlayOptions overlayOptions = new MarkerOptions().position(latLng).icon(bitmapDescriptor)
                .zIndex(9).draggable(false);
        mBaiduMap.addOverlay(overlayOptions);

    }

    @Override
    protected String getTitleBarText() {
        return "店铺位置";
    }

}
